package com.chang.controller;

import com.chang.BaseController;
import com.chang.common.DataBase;
import com.jfinal.core.ActionKey;
import com.mongodb.BasicDBObject;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class User extends BaseController {
    @ActionKey("/User")
    public void ToScoreSource(){
        ReturnPage("/jsp/User.jsp", "");
    }

    /**
     * 列表
     * @throws UnsupportedEncodingException
     */
    @ActionKey("/User/table")
    public void getUser() throws UnsupportedEncodingException {
        BasicDBObject SortObject = new BasicDBObject();
        SortObject.put("createday",1);//排序，1为正序，-1为倒序
        String key = getPara("key");
        BasicDBObject FilterObject=null;
        if(StringUtils.isNotEmpty(key)){
            key = key.trim();
            if (key.equals(new String(key.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                key = new String(key.getBytes("ISO-8859-1"), "utf-8");
            }
            //定义KEY模糊查询
            Pattern pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
            FilterObject = new BasicDBObject().append("$or",
                    new BasicDBObject[]{
                            new BasicDBObject("real_name", pattern)
                    });
        }
        List<Document> DocmentList = DataBase.find("user",FilterObject,SortObject);
        if(DocmentList!=null && DocmentList.size()>0)
        {
            this.ReturnJSON(DocmentList);
        }
        else
        {
            this.ReturnJSON("");
        }
    }
    /**
     * 用户添加
     */
    @ActionKey("/User/Add")
    public void UserAdd()
    {
        Document CommitObject = FormToDocument(getRequest(),getParaNames());
        BasicDBObject FilterObject = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("account",CommitObject.get("account")),
                }
        );
        List<Document> UserList = DataBase.find("user", FilterObject, null);
        if (UserList!=null&&UserList.size()>0){
            ReturnJSON("-1","用户已经存在","");
        }else {
            CommitObject.append("_id",DataBase.getSequence("user"));
            CommitObject.append("createday", new Date());
            DataBase.InsertNotAutoID("user", CommitObject);
            ReturnJSON("0","",null);
        }
    }
    /**
     * 根据id查询用户
     */
    @ActionKey("/User/ReaderOne")
    public void ReaderOne()
    {
        String _id = getPara("_id");
        BasicDBObject FilterObject = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(_id))
                });
        List<Document> DocmentList =DataBase.find("user", FilterObject,null);
        ReturnJSON("0","",DocmentList);
    }
    /**
     * 编辑用户
     */
    @ActionKey("/User/CommitEdit")
    public void update()
    {
        Document CommitObject = FormToDocument(getRequest(),getParaNames());
        BasicDBObject UpdateFilter = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(CommitObject.getString("_id")))

                });
        List<Document> checkList=DataBase.find("user", UpdateFilter, null);
        if(checkList!=null && checkList.size()>0)
        {
            CommitObject.remove("_id");
            DataBase.updateByFilter("user", UpdateFilter, CommitObject);
            ReturnJSON("0","",null);
        }
        else
        {
            ReturnJSON("-1","找不到该用户！请确认该用户是否存在！",null);
        }
    }

    @ActionKey("/User/Remove")
    public void delete(){
        String _id = getPara("_id");
        BasicDBObject FilterObject = new BasicDBObject().append("$and",
                new BasicDBObject[]{
                        new BasicDBObject("_id",Integer.parseInt(_id))
                });
        DataBase.deleteByFilter("user", FilterObject);
        ReturnJSON("0","",null);
    }
}
